import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
import { useAuthStore } from '../store/auth';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/edit-profile',
    name: 'EditProfile',
    component: () => import('../views/EditProfile.vue'),
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
  },

  {
    path: '/me',
    name: 'Profile',
    component: () => import('../views/Profile.vue'),
  },

  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // 获取认证状态
  const authStore = useAuthStore();
  
  // 如果访问的是Profile页面且用户未登录
  if (to.name === 'Profile' && !authStore.isLoggedIn) {
    // 重定向到登录页面
    next('/login');
  } else {
    // 其他情况正常放行
    next();
  }
});

export default router;